﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.SalaryRules
{
    public partial class SalaryRulesInputFields : SERP.Modules.ModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!this.IsPostBack) && Request["SalaryRuleId"] != null)
            {
                BindItems();
            }
            LoadLanguage();
        }
        private void BindItems()
        {
            SalaryRuleItemsController controller = new SalaryRuleItemsController();
            controller.LoadAllDSSalaryRuleItems(Controller.ConvertInt(Request["SalaryRuleId"]));
            gridData.DataSource = controller.Table;
            gridData.DataBind();
        }
        private SalaryRulesEntity model = new SalaryRulesEntity();
        public SalaryRulesEntity Model
        {
            set
            {
                model = value;
            }
            get
            {
                return model;
            }
        }
        //Load All lable text
        private void LoadLanguage()
        {
            labelRuleName.Text = SalaryRulesStringMapping.RuleName + ":";
            labelNote.Text = SalaryRulesStringMapping.Note + ":";
            labelStatus.Text = SalaryRulesStringMapping.Status + ":";
            validRQStatus.ErrorMessage = Constants.APP_REQUIRE_WARNING;
            validExpressionStatus.ErrorMessage = Constants.APP_REQUIRE_INTERGER_WARNING;
            labelFromDay.Text = SalaryRulesStringMapping.FromDay + ":";
            validRQFromDay.ErrorMessage = Constants.APP_REQUIRE_WARNING;
            validExpressionFromDay.ErrorMessage = Constants.APP_REQUIRE_INTERGER_WARNING;
            labelToDay.Text = SalaryRulesStringMapping.ToDay + ":";
            validRQToDay.ErrorMessage = Constants.APP_REQUIRE_WARNING;
            validExpressionToDay.ErrorMessage = Constants.APP_REQUIRE_INTERGER_WARNING;
            lblInfo.Text = Constants.APP_INFO + " " + SalaryRulesStringMapping.SalaryRules;
            lblItem.Text = Constants.APP_INFO + " " + SalaryRuleItemsStringMapping.SalaryRuleItems;
            btnAdd.Text = Constants.APP_ADD;
        }
        public void UpdateToModel()
        {
            model.RuleName = Convert.ToString(txtRuleName.Text);
            model.Note = Convert.ToString(txtNote.Text);
            model.Status = Convert.ToByte(txtStatus.Text);
            model.FromDay = Convert.ToInt32(txtFromDay.Text);
            model.ToDay = Convert.ToInt32(txtToDay.Text);
        }
        public void UpdateToScreen()
        {
            txtRuleName.Text = model.RuleName;
            txtNote.Text = model.Note;
            txtStatus.Text = Convert.ToString(model.Status);
            txtFromDay.Text = Convert.ToString(model.FromDay);
            txtToDay.Text = Convert.ToString(model.ToDay);
        }
        public void ClearScreen()
        {
            txtRuleName.Text = "";
            txtNote.Text = "";
            txtStatus.Text = "0";
            txtFromDay.Text = "0";
            txtToDay.Text = "0";
        }
        private DataTable GetChilds()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("SalaryRuleItemId", typeof(int));
            tbl.Columns.Add("SalaryRuleId", typeof(int));
            tbl.Columns.Add("SalaryFieldId", typeof(int));
            tbl.Columns.Add("CalculationType", typeof(int));
            tbl.Columns.Add("CalCulationIndex", typeof(int));
            tbl.Columns.Add("RuleContent", typeof(string));
            foreach (RepeaterItem item in gridData.Items)
            {
                DataRow r = tbl.NewRow();
                Label lbl = (Label)item.FindControl("lblID");
                Modules.SalaryRules.CalculationTypes txtCalculationType = (Modules.SalaryRules.CalculationTypes)item.FindControl("txtCalculationType");
                Modules.SalaryFields.DropDownListSalaryFields txtSalaryFieldId = (Modules.SalaryFields.DropDownListSalaryFields)item.FindControl("txtSalaryFieldId");
                TextBox txtIndex = (TextBox)item.FindControl("txtIndex");
                TextBox txtContent = (TextBox)item.FindControl("txtContent");
                r["SalaryRuleItemId"] = Controller.ConvertInt(lbl.Text);
                r["CalculationType"] = Controller.ConvertInt(txtCalculationType.SelectedValue);
                r["SalaryFieldId"] = Controller.ConvertInt(txtSalaryFieldId.SelectedValue);
                r["CalCulationIndex"] = Controller.ConvertInt(txtIndex.Text);
                r["RuleContent"] = txtContent.Text;
                tbl.Rows.Add(r);
            }
            return tbl;
        }
        public void SaveChilds(int salaryRuleId)
        {
            DataTable tbl = GetChilds();
            SalaryRuleItemsController controller = new SalaryRuleItemsController();
            foreach (DataRow r in tbl.Rows)
            {
                controller.ResetObject();
                controller.SalaryRuleItemsObject = controller.MapDataRowToObject(r);
                controller.SalaryRuleItemsObject.SalaryRuleId = salaryRuleId;
                if (controller.SalaryRuleItemsObject.SalaryRuleItemId > 0)
                    controller.Update();
                else
                    controller.AddNew();
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable tbl = GetChilds();
            DataRow r = tbl.NewRow();
            tbl.Rows.Add(r);
            gridData.DataSource = tbl;
            gridData.DataBind();
        }
        public bool ValidateScreen()
        {
            bool result = true;
            return result;
        }
        protected void btnXoa_OnCommand(object sender, CommandEventArgs e)
        {
            int i = Controller.ConvertInt(e.CommandArgument);
            DataTable tbl = GetChilds();
            DataRow r = tbl.Rows[i];
            if (Controller.ConvertInt(r["SalaryRuleItemId"]) > 0)
            {
                SalaryRuleItemsController controller = new SalaryRuleItemsController();
                controller.DeleteByID(Controller.ConvertInt(r["SalaryRuleItemId"]));
            }
            tbl.Rows.RemoveAt(i);
            gridData.DataSource = tbl;
            gridData.DataBind();
        }
    }
}
